﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.Model.Entity.UserCenter;
using Kovan.Model.Parameter.UserCenter;
using Kovan.Model.ViewModel.UserCenter;
using Kovan.Model.Common;

namespace Kovan.DAL.UserCenter
{
    public class DUserManage
    {
        /// <summary>
        /// 获取用户列表数据
        /// </summary>
        /// <param name="param">用户参数实体</param>
        /// <returns></returns>
        public IEnumerable<EUser> GetUserItems(PUser param)
        {
            using (var db = new MysqlContext())
            {
                //db.Database.SqlQuery(null, string.Empty, null);
                var query = from u in db.act_user
                            join d in db.act_dept on u.dept_id.Value equals d.dept_id into left
                            from cleft in left.DefaultIfEmpty()
                            orderby u.user_id descending
                            select new EUser
                            {
                                user = u,
                                dept = cleft
                            };

                if (param.userId.HasValue)
                {
                    query = query.Where(o => o.user.user_id.Value == param.userId.Value);
                }
                if (!string.IsNullOrEmpty(param.loginName))
                {
                    query = query.Where(o => o.user.login_name.Contains(param.loginName));
                }
                if (!string.IsNullOrEmpty(param.userName))
                {
                    query = query.Where(o => o.user.name.Contains(param.userName));
                }

                if (param.deptId.HasValue)
                {
                    query = query.Where(o => o.dept.dept_id == param.deptId.Value);
                }

                if (!string.IsNullOrEmpty(param.deptName))
                {
                    query = query.Where(o => o.dept.name.Contains(param.deptName));
                }
                if (param.applicationId.HasValue)
                {
                    query = query.Where(o => o.user.application_id.Value == param.applicationId);
                }
                if (!string.IsNullOrEmpty(param.identityCard))
                {
                    query = query.Where(o => o.user.identity_card.Contains(param.identityCard));
                }
                if (param.enabled.HasValue)
                {
                    query = query.Where(o => o.user.enabled.Value == param.enabled.Value);
                }
                if (!string.IsNullOrEmpty(param.keyword))
                {
                    query = query.Where(o => o.user.name.Contains(param.keyword) || o.user.login_name.Contains(param.keyword));
                }
                //总条数
                param.TotalRows = query.Count();

                if (param.PageSize > 0 && param.PageIndex > 0)
                {
                    query = query.Skip(param.PageSize * (param.PageIndex - 1)).Take(param.PageSize);
                }

                return query.OrderByDescending(c => c.user.user_id.Value).ToList();
            }
        }

        public EUser GetUserModel(string ukey)
        {
            using (var db = new MysqlContext())
            {
                var query = from u in db.act_user
                            where u.ukey == ukey
                            join a in db.act_application on u.application_id.Value equals a.application_id.Value
                            join ur in db.act_user_role on u.user_id.Value equals ur.user_id.Value
                            join r in db.act_role on ur.role_id.Value equals r.role_id.Value
                            join d in db.act_dept on u.dept_id.Value equals d.dept_id into left
                            from cleft in left.DefaultIfEmpty()
                            orderby u.user_id descending
                            select new EUser
                            {
                                user = u,
                                dept = cleft,
                                role = r,
                                application = a
                            };
                return query.FirstOrDefault();
            }
        }

        public IEnumerable<act_role> GetUserRoles(string ukey)
        {
            using (var db = new MysqlContext())
            {
                var query = from u in db.act_user
                            where u.ukey == ukey
                            join ur in db.act_user_role on u.user_id.Value equals ur.user_id.Value
                            join r in db.act_role on ur.role_id.Value equals r.role_id.Value
                            select r;
                return query;
            }
        }

        /// <summary>
        /// 根据角色ID获取菜单
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public IEnumerable<Menu> GetUserMenu(int roleid)
        {
            using (var db = new MysqlContext())
            {
                var sql = @"SELECT m.name AS Name,m.url AS Url,m.icon_url AS IconUrl,m.is_view AS IsView,m.parent_id AS ParentID,m.ukey AS ModuleUkey,m.menu_params AS ModuleParams,GROUP_CONCAT(a.name) AS Authoritys FROM `act_module` AS m 
                            INNER JOIN `act_authority` AS a ON m.`module_id`=a.`module_id` 
                            INNER JOIN `act_role_authority` AS ra ON a.`authority_id`=ra.`authority_id`
                            INNER JOIN `act_role` AS r ON ra.`role_id`=r.`role_id` WHERE r.role_id={0} GROUP BY m.module_id";
                sql = string.Format(sql, roleid);
                return db.Database.SqlQuery<Menu>(sql).ToList();
            }
        }

        /// <summary>
        /// 根据角色ID获取权限
        /// </summary>
        /// <param name="roid_id"></param>
        /// <returns></returns>
        public IEnumerable<ERoleAuthority> GetRoleAuthority(int roid_id)
        {
            using (var db = new MysqlContext())
            {
                var query = from ra in db.act_role_authority
                            where ra.role_id == roid_id
                            join a in db.act_authority on ra.authority_id.Value equals a.authority_id.Value
                            select new ERoleAuthority
                            {
                                authority_id = ra.authority_id,
                                authority_name = a.name,
                                authority_code=a.code,
                                module_id = a.module_id,
                                role_id = ra.role_id,
                                authority_code_order=a.code_order
                            };
                return query.ToList();
            }
        }

        //public IEnumerable<act_authority> GetUserAuthoritys()
        //{
        //    return null;
        //}

        public void GetUserAccesspoint()
        {

        }


    }
}
